package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.controller.dto.AfterSaleCommitTask;
import cn.stylefeng.guns.modular.system.entity.AfterIndentCheck;
import cn.stylefeng.guns.modular.system.entity.AfterIndentCommit;
import cn.stylefeng.guns.modular.system.entity.AfterIndentOrder;
import cn.stylefeng.guns.modular.system.model.params.AfterIndentCheckParam;
import cn.stylefeng.guns.modular.system.service.AfterIndentCheckService;
import cn.stylefeng.guns.modular.system.service.AfterIndentOrderService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.core.util.common.SystemConstant;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 售后单控制器
 *
 * @author 小飞
 * @Date 2019-07-10 16:55:42
 */
@Controller
@RequestMapping("/afterIndentCheck")
public class AfterIndentCheckController extends BaseController {

    private String PREFIX = "/afterIndentCheck";

    @Autowired
    private AfterIndentCheckService afterIndentCheckService;
    @Resource
    private TaskService taskService;
    @Autowired
    private AfterIndentOrderService afterIndentOrderService;
    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-07-10
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/afterIndentCheck.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-07-10
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/afterIndentCheck_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-07-10
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/afterIndentCheck_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-07-10
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(AfterIndentCheckParam afterIndentCheckParam) {
        this.afterIndentCheckService.add(afterIndentCheckParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-07-10
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(AfterIndentCheckParam afterIndentCheckParam) {
        this.afterIndentCheckService.update(afterIndentCheckParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-07-10
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(AfterIndentCheckParam afterIndentCheckParam) {
        this.afterIndentCheckService.delete(afterIndentCheckParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-07-10
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(AfterIndentCheckParam afterIndentCheckParam) {
        AfterIndentCheck detail = this.afterIndentCheckService.getById(afterIndentCheckParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-07-10
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(AfterIndentCheckParam afterIndentCheckParam) {
        List<Task> tasks = taskService.createTaskQuery().processDefinitionKey(SystemConstant.afterSaleOrderv).taskCandidateUser(ShiroKit.getUserNotNull().getId() + "").list();

        if (tasks.size() == 0){
            return  new LayuiPageInfo();
        }

        List<AfterSaleCommitTask> afterSaleCommitTasks = tasks.stream().filter(task -> afterIndentOrderService.getOne(new QueryWrapper<AfterIndentOrder>()
                .lambda().eq(AfterIndentOrder::getProcessInstanceId,task.getProcessInstanceId())) !=null).map(task -> {

            AfterSaleCommitTask afterSaleCommitTask = new AfterSaleCommitTask();
            AfterIndentOrder afterIndentOrder = afterIndentOrderService.getOne(new QueryWrapper<AfterIndentOrder>()
                    .lambda().eq(AfterIndentOrder::getProcessInstanceId,task.getProcessInstanceId()));
            BeanUtils.copyProperties(afterIndentOrder,afterSaleCommitTask);
            afterSaleCommitTask.setTypeStr(afterIndentOrder.getType() == 1 ? "维修":"售后");
            afterSaleCommitTask.setProcessInstanceId(task.getProcessInstanceId());
            afterSaleCommitTask.setTaskId(task.getId());
            afterSaleCommitTask.setDealerName(userService.getById(afterIndentOrder.getUserId()).getName());
            return  afterSaleCommitTask;
        }).collect(Collectors.toList());
        LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
        layuiPageInfo.setCount(afterSaleCommitTasks.size());
        layuiPageInfo.setData(afterSaleCommitTasks);
        return  layuiPageInfo;
    }

}


